<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="token-granter-container">
  <a [routerLink]="['..']" class="back-link"><small><< Back to extension grants</small></a>
  <h1>{{extensionGrant.name}}</h1>
  <div *ngIf="extensionGrantSchema && extensionGrantSchema.description" class="token-granter-contextual-help">
    <pre [innerHTML]="extensionGrantSchema?.description"></pre>
  </div>
  <div fxLayout="row">
    <div class="token-granter-content" fxLayout="column" fxFlex="70">
      <form #tokenGranterForm="ngForm">
        <div class="gv-form-section">
          <mat-form-field appearance="outline" floatLabel="always">
            <mat-label>Name</mat-label>
            <input matInput type="text" placeholder="Name" name="name" [(ngModel)]="extensionGrant.name" required [readonly]="!editMode">
            <mat-hint>Extension grant name</mat-hint>
          </mat-form-field>
          <mat-form-field appearance="outline" floatLabel="always">
            <mat-label>Grant type</mat-label>
            <input matInput type="text" placeholder="Grant type" name="grantType" [(ngModel)]="extensionGrant.grantType" [pattern]="rfc3986_absolute_URI" required disabled>
            <mat-hint align="end">OAuth 2.0 grant type. The client uses an extension grant type by specifying the grant type value of the "grant_type" parameter of the token endpoint.</mat-hint>
          </mat-form-field>
        </div>

        <div class="gv-form-section">
          <div class="gv-form-section-title">
            <h5>User creation</h5>
            <mat-divider></mat-divider>
          </div>
          <div fxLayout="column">
            <mat-slide-toggle
              (change)="enableCreateUser($event)"
              [checked]="extensionGrant.createUser" [disabled]="!editMode">
              Create user account
            </mat-slide-toggle>
            <mat-hint style="font-size: 75%;">Used for user account activity and OAuth 2.0 refresh token flow.</mat-hint>
          </div>
        </div>

        <div class="gv-form-section">
          <div class="gv-form-section-title">
            <h5>Check user</h5>
            <mat-divider></mat-divider>
          </div>
          <div fxLayout="column">
            <mat-slide-toggle
              (change)="enableCheckUser($event)"
              [checked]="extensionGrant.userExists" [disabled]="!editMode">
              User existence
            </mat-slide-toggle>
            <mat-hint style="font-size: 75%;">Check that the user is existing in the identity provider.</mat-hint>
          </div>
        </div>

        <div class="gv-form-section">
          <div class="gv-form-section-title">
            <h5>Identity provider</h5>
            <mat-divider></mat-divider>
          </div>
          <div>
            <mat-form-field appearance="outline" floatLabel="always">
              <mat-select placeholder="Identity provider" name="identityProvider" [(ngModel)]="extensionGrant.identityProvider" [disabled]="!editMode">
                <mat-option *ngFor="let identityProvider of identityProviders" [value]="identityProvider.id">{{ identityProvider.name }}</mat-option>
              </mat-select>
              <mat-hint>Identity provider used to retrieve user information from incoming request assertion.</mat-hint>
            </mat-form-field>
          </div>
        </div>

        <div class="gv-form-section">
          <div class="gv-form-section-title">
            <h5>Configuration</h5>
            <mat-divider></mat-divider>
          </div>
          <div>
            <extension-grant-form *ngIf="extensionGrant" [extensionGrantConfiguration]="extensionGrantConfiguration" [extensionGrantSchema]="extensionGrantSchema" (configurationCompleted)="enableTokenGranterUpdate($event)"></extension-grant-form>
          </div>
        </div>

        <div fxLayout="row" *ngIf="editMode">
          <button mat-raised-button [disabled]="(!tokenGranterForm.valid || tokenGranterForm.pristine) && (!configurationIsValid || configurationPristine) && !formChanged" (click)="update()">SAVE</button>
        </div>
      </form>

      <div *hasPermission="['domain_extension_grant_delete']" class="token-granter-settings-delete-zone" fxLayout="column">
        <p>This operation cannot be undone.</p>
        Extension grant will be deleted, applications will not be able to use this extension grant anymore.
        <div class="provider-settings-delete-button" fxLayout="row">
          <span fxFlex></span>
          <button mat-raised-button color="warn" (click)="delete($event)">DELETE</button>
        </div>
      </div>
    </div>
    <div class="token-granter-description" fxFlex>
      <h3>Manage extension grant</h3>
      <div class="token-granter-description-content">
        <p>
          Configure new extension grant to request OAuth 2.0 access tokens. OAuth 2.0 specification defines four
          grant types : authorization code, implicit, resource owner password credentials, and client credentials. Extension grant act as an extensibility
          mechanism for defining additional types.
        </p>
      </div>
    </div>
  </div>
</div>
